﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.UI;
using Kevin.Shop.Business.Products.Components.Content;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using System.Collections.Specialized;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Comments.Components;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.Modle.Product;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Order;

namespace RiderShop.WebSite.Controllers.MyAccount
{
    public partial class MyAccountController : ThemeController
    {
        #region My Comment
        /// <summary>
        /// 得到用户的评论列表
        /// </summary>
        /// <param name="pageIndex">当前页的索引</param>
        /// <returns>评论集合</returns>
        [Authorize]
        [HttpGet]
        public ActionResult MyComment(int pageIndex = 0)
        {
            MyCommentModel myCommentModel = new MyCommentModel();
            DbQueryResult<List<OrderCustomerReviewInfo>> result = myCommentModel.GetUserComments(pageIndex, WebClientContext.CON_PageSize);
            if (result != null && result.Data.Count > 0)
            {
                myCommentModel.CommentList = result.Data;

                return View(WebClientContext.Current.SiteTheme, "MyAccount", "MyComment", WebClientContext.Current.CacheKeyDefaultMasterPage, myCommentModel);
            }

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "MyComment", WebClientContext.Current.CacheKeyDefaultMasterPage, myCommentModel);
        }

        /// <summary>
        /// 得到用户的评论列表
        /// </summary>
        /// <param name="pageIndex">当前页的索引</param>
        /// <returns>评论集合</returns>
        [HttpGet]
        public ActionResult MyCommentAjaxRequest(int pageIndex)
        {
            MyCommentModel myCommentModel = new MyCommentModel();
            DbQueryResult<List<OrderCustomerReviewInfo>> result = myCommentModel.GetUserComments(pageIndex, WebClientContext.CON_PageSize);
            if (result != null && result.Data.Count > 0)
            {
                myCommentModel.CommentList = result.Data;

                return PartialView("MyCommentPartial", myCommentModel);
            }
            return PartialView("MyCommentPartial", myCommentModel);
        }
        #endregion

        #region Publish Comment
        /// <summary>
        ///  发表评论
        /// </summary>
        /// <param name="prd">商品编号</param>
        /// <param name="orderNumber">订单编号</param>
        /// <returns>视图</returns>
        [Authorize]
        [HttpGet]
        public ActionResult PublishComment(string prd, string orderNumber)
        {
            ProductModel productModel = new ProductModel();
            productModel.GetProductByProductId(prd);
            productModel.OrderNumber = orderNumber;

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "PublishComment", WebClientContext.Current.CacheKeyDefaultMasterPage, productModel);
        }

        /// <summary>
        /// 发表评论适用于表单提交形式
        /// </summary>
        /// <param name="comment">发表的评价信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult PublishComment(ProductReviewInfo comment)
        {
            ProductModel productModel = new ProductModel();
            productModel.GetProductByProductId(comment.ProductId);
            productModel.OrderNumber = comment.OrderNumber;

            MyCommentModel myCommentModel = new MyCommentModel();
            ServiceResponser serviceResponser = myCommentModel.CreateProductComment(comment);
            productModel.Result = serviceResponser;
            //修改失败,返回页面,页面靠Result显示出失败的信息
            if (productModel.Result != null && !productModel.Result.Status)
            {
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "PublishComment", WebClientContext.Current.CacheKeyDefaultMasterPage, productModel);
            }

            return Redirect("~/MyAccount/MyComment");
        }

        /// <summary>
        /// 发表评论适用于ajax请求形式
        /// </summary>
        /// <param name="comment">发表的评价信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public JsonResult PublishCommentAjaxRequest(ProductReviewInfo comment)
        {
            ServiceResponser serviceResponser;
            MyCommentModel myCommentModel = new MyCommentModel();
            serviceResponser = myCommentModel.CreateProductComment(comment);

            return Json(serviceResponser, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
